#include "stdafx.h"
#include "VFWGrab.h"
#include "mainframe.h"
#include "FTPTransferDlg.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

BEGIN_MESSAGE_MAP(CVFWGrabApp, CWinApp)
	//{{AFX_MSG_MAP(CVFWGrabApp)
	//}}AFX_MSG
	ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

CVFWGrabApp::CVFWGrabApp()
{
}

CVFWGrabApp theApp;

BOOL CVFWGrabApp::InitInstance()
{
  //Initialize GDI+
  Gdiplus::GdiplusStartupInput gdiplusStartupInput;
  if (Gdiplus::GdiplusStartup(&m_GDIplusToken, &gdiplusStartupInput, NULL) != Gdiplus::Ok)
  {
    AfxMessageBox(_T("Failed to initialize GDI+"));
    return FALSE;
  }

	// Parse command line for the filename of the ini file
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);
  if (cmdInfo.m_strFileName.GetLength())
  {
    free((void*)m_pszProfileName);
    m_pszProfileName = _tcsdup(cmdInfo.m_strFileName);
  }
  else
  {
    //Set the ini file to be the same name as the exe located where the exe
    //is running from
    TCHAR pszIni[_MAX_PATH];
    GetModuleFileName(NULL, pszIni, _MAX_PATH);
    TCHAR pszDrive[_MAX_DRIVE];
    TCHAR pszDir[_MAX_DIR];
    TCHAR pszFname[_MAX_FNAME];
    _tsplitpath(pszIni, pszDrive, pszDir, pszFname, NULL);
    _tmakepath(pszIni, pszDrive, pszDir, pszFname, _T("ini"));
    free((void*)m_pszProfileName);
    m_pszProfileName = _tcsdup(pszIni);
  }

  //Create the mainframe window which has the VFW capture window
  CMainFrame* pMainframe = new CMainFrame;
  if (!pMainframe->LoadFrame(IDR_MAINFRM))
    return FALSE;
  m_pMainWnd = pMainframe;
	m_pMainWnd->ShowWindow(m_nCmdShow);
	m_pMainWnd->UpdateWindow();

	return TRUE;
}

int CVFWGrabApp::ExitInstance()
{
  //Close down GDI+
  Gdiplus::GdiplusShutdown(m_GDIplusToken);
  
  //Let the parent class do its thing
  return CWinApp::ExitInstance();
}